<?php	
class Obj_Country {
	
	protected static $t;
	
	public static function init() {
		self::$t = new Obj_DBTable_Countries();
	}
	
	public static function getCountries($activeOnly = false) {
		$ar_ = self::$t->getCountries($activeOnly);
		$ar = array();
		foreach ($ar_ as $k => $v) {
			$ar[$v['ID']] = $v;
		}
		return $ar;
	}
	
	public static function getFullCountryList($activeOnly = false) {
		$ar_ = self::$t->getFullCountryList($activeOnly);
		return $ar_;
	}
	
	public static function getExtendedCountries() {
		$ar_ = self::$t->getExtendedCountries();
		$ar = array();
		foreach ($ar_ as $v) {
			if (isset($ar[$v['ID']])) {
				$ar[$v['ID']]['count'][(int)$v['Attribute']] = (int) $v['objc'];
			}
			else {
				$ar[$v['ID']] = array_merge($v, array('count' => array(OBJECT_SALE => 0, OBJECT_RENT => 0, OBJECT_INVESTMENT_PROJECT => 0)));
				$ar[$v['ID']]['count'][(int)$v['Attribute']] = (int) $v['objc'];
				$ar[$v['ID']]['pages'] = Obj_LocationContentPage::getLocationContentPages($v['ID'], CONTENT_TYPE_COUNTRY, true); 
			}
		}
		return $ar;
	}
	
	public static function saveCountry($arData, $id) {
		if ($id == 0) {
			return self::$t->insert($arData);
		}
		else self::$t->update($arData, 'ID = '.intval($id));
		return intval($id);
	}
	
	public static function getCountry($id) {
		return self::$t->getCountry((int) $id);
	}
	
	public static function getAliasCountry($alias) {
		return self::$t->getAliasCountry($alias);
	}
	
	public static function delCountry($id){
		return self::$t->delete('ID = '.intval($id));
	}
	
} Obj_Country::init();

